/**
 * 
 */
package com.genITeam.ria.bl;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.genITeam.ria.dao.ChatRoomDAO;
import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.vo.ChatMessageVo;
import com.genITeam.ria.vo.Member;

/**
 * @author 05030045
 *
 */
public class ChatRoomBL {

	public String getUserList (ArrayList roomUsers, int roomId){
		System.out.println("In ChatRoomBL " + roomUsers.size() + " users logged in") ; 
    	ArrayList users = new ArrayList();
    	String memberId = null ;
    	String room = null ;
    	StringTokenizer stk = null ;
    	ChatRoomDAO roomDAO = new ChatRoomDAO () ;
    	Transaction tx = null;
    	String xml = "" ;
    	try{
	    	Session session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
	    	
	    	for (int i = 0 ; i < roomUsers.size(); i ++){
	    		stk = new StringTokenizer ((String)roomUsers.get(i),",") ;
	    		
	    		room = stk.nextToken() ;
	    		memberId = stk.nextToken() ;
	    		
	    		if (Integer.parseInt(room) == roomId){
	    			users.add(roomDAO.getMember(Integer.parseInt(memberId),session)); 
	    		}    		
	    	}
	    	xml = toXML(users) ;
	    	tx.commit();
    	}catch(Exception e) {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} 
		}
    	
    	System.out.println(xml) ;
    	
    	return xml ;
    }
	
	public Member getMember (int memberId){
		ChatRoomDAO roomDAO = new ChatRoomDAO () ;
    	Transaction tx = null;
    	Member member = null ;
    	try{
	    	Session session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			
			member = roomDAO.getMember(memberId, session) ;
	    
    	}catch(Exception e) {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} 
		}
    	return member ;
	}
	
	public String getMessages (ArrayList chatMessages, int roomId, int messageId, int memberId){
		System.out.println("In ChatRoomBL.getMessages Total messages are " + chatMessages.size()) ;
		System.out.println("Parameters are roomId=" + roomId + " MessageId=" + messageId + " MemberId=" + memberId) ;
		ArrayList msgs = new ArrayList () ;
		ChatMessageVo chatMsg = null ;
		for (int i = chatMessages.size(); (i > messageId && msgs.size() < 10); i --){
			chatMsg = (ChatMessageVo) chatMessages.get(i - 1) ;
			System.out.println("Message=" + chatMsg.getMemberId() + " Member=" + chatMsg.getMemberId() ) ;
			if (chatMsg.getRoomId() == roomId && chatMsg.getMemberId() != memberId){
				msgs.add(0, chatMsg) ;
			}
		}
		
		while(chatMessages.size() > 50){
			chatMessages.remove(0) ;			
		}
		
		return generateXML (msgs) ;
	}
	
	public String generateXML (ArrayList msgs){
		StringBuffer xml = new StringBuffer ("<recordset>") ;
		ChatMessageVo chatMsg = null ;
		for (int i = 0 ; i < msgs.size() ; i ++){
			chatMsg = (ChatMessageVo) msgs.get(i) ;
			xml.append("<message messageId=\"" + chatMsg.getMessageId() + "\" message=\"" + chatMsg.getMessage() + "\" />") ;			
		}		
		
		xml.append("</recordset>");
		
		System.out.println(xml.toString()) ;
		return xml.toString();
	}
	
	public String toXML (ArrayList u){
		StringBuffer xml = new StringBuffer ("<contact>\n") ;
		Member member = null ;
		
		for (int i = 0 ; i < u.size() ; i ++){
			member = (Member) u.get(i) ;
			xml.append("<item>");
			xml.append("<id>" + member.getId() + "</id>");
			xml.append("<firstName>" + member.getFirstName()
					+ "</firstName>");
			xml.append("<lastName>" + member.getLastName() + "</lastName>");
			xml.append("<nick>" + member.getNick() + "</nick>");
			xml.append("<email>" + member.getEmail() + "</email>");
			xml.append("<phone>" + member.getPhone() + "</phone>");
			xml.append("<address>" + member.getAddress() + "</address>");
			xml.append("<joinedOn>" + member.getJoinedOn() + "</joinedOn>");
			xml.append("<userName>" + member.getUserName() + "</userName>");
			xml.append("</item>");
			xml.append("\n");

		}
				
		xml.append("</contact>");
		return xml.toString();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ChatRoomBL cab = new ChatRoomBL () ;
		ArrayList al = new ArrayList() ;
		al.add("307,1") ;
		al.add("307,2") ;
		al.add("307,3") ;
		al.add("307,4") ;
		System.out.println(cab.getUserList(al, 307)) ;

	}

}
